package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhunongzonghe.beans.ZnOrderBean;
import com.example.zhunongzonghe.beans.ZnOrderDetailsBean;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnOrder;
import com.example.zhunongzonghe.entity.ZnOrderDetails;
import com.example.zhunongzonghe.service.IZnOrderDetailsService;
import com.example.zhunongzonghe.service.IZnOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;


@RestController
@RequestMapping("/znOrder")
public class ZnOrderController {
    @Autowired
    private IZnOrderService znOrderService;
    @Autowired
    private IZnOrderDetailsService iZnOrderDetailsService;


    /**
     * 后台：分页查询全部
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/queryOrderAll")
    public Result queryOrderAll(@RequestParam("currentPage") Integer currentPage,
                                @RequestParam("pageSize") Integer pageSize,
                                @RequestParam("input") Object searchInfo){
        if (currentPage==null && pageSize==null) new Result<>(false,"数据异常",400);
        Page<ZnOrderBean> znOrderPage = znOrderService.queryOrderAll(currentPage,pageSize,searchInfo);
        return new Result<>(true,"查询成功",200,znOrderPage);
    }

    /**
     * 后台全部未支付订单
     * @param currentPage
     * @param pageSize
     * @param searchInfo
     * @return
     */
    @GetMapping("/nonPaymentAll")
    public Result selectPayAll(@RequestParam("currentPage") Integer currentPage,
                            @RequestParam("pageSize") Integer pageSize,
                            @RequestParam("input") Object searchInfo){
        if (currentPage==null && pageSize==null) new Result<>(false,"数据异常",400);
        Page<ZnOrderBean> orderBeanPage = znOrderService.nonPaymentAll(currentPage,pageSize,searchInfo);
        return new Result(true,"成功",200,orderBeanPage);
    }

    /**
     * 后台全部未发货订单
     * @param currentPage
     * @param pageSize
     * @param searchInfo
     * @return
     */
    @GetMapping("/unfilledOrders")
    public Result selectUnshipped(@RequestParam("currentPage") Integer currentPage,
                                  @RequestParam("pageSize") Integer pageSize,
                                  @RequestParam("input") Object searchInfo){
        if (currentPage==null && pageSize==null) new Result<>(false,"数据异常",400);
        Page<ZnOrderBean> orderBeanPage = znOrderService.notYetShipped(currentPage,pageSize,searchInfo);
        return new Result(true,"成功",200,orderBeanPage);
    }

    /**
     * 后台全部未收货订单
     * @param currentPage
     * @param pageSize
     * @param searchInfo
     * @return
     */
    @GetMapping("/getNotReceive")
    public Result selectNotReceive(@RequestParam("currentPage") Integer currentPage,
                                   @RequestParam("pageSize") Integer pageSize,
                                   @RequestParam("input") Object searchInfo){
        if (currentPage==null && pageSize==null) new Result<>(false,"数据异常",400);
        Page<ZnOrderBean> orderBeanPage = znOrderService.notReceive(currentPage,pageSize,searchInfo);
        return new Result(true,"成功",200,orderBeanPage);
    }

    /**
     * 后台全部未收货订单
     * @param currentPage
     * @param pageSize
     * @param searchInfo
     * @return
     */
    @GetMapping("/AllNoRefund")
    public Result AllNoRefund(@RequestParam("currentPage") Integer currentPage,
                                   @RequestParam("pageSize") Integer pageSize,
                                   @RequestParam("input") Object searchInfo){
        if (currentPage==null && pageSize==null) new Result<>(false,"数据异常",400);
        Page<ZnOrderBean> orderBeanPage = znOrderService.AllNoRefund(currentPage,pageSize,searchInfo);
        return new Result(true,"成功",200,orderBeanPage);
    }

    /**
     * 分页查询当前登录用户订单
     * @param principal
     * @param pageId
     * @param pageSize
     * @return
     */
    @GetMapping("/OrderAndDetails")
    public Result<IPage> OrderAndDetails(Principal principal,
                                         @RequestParam("pageId") Integer pageId,
                                         @RequestParam("pageSize") Integer pageSize){
        IPage<ZnOrderBean> znOrderBeanList = znOrderService.queryOrderAllWithPage(principal.getName(), pageId,pageSize);
        return new Result<>(true,"查询成功",200,znOrderBeanList);
    }

    /**
     * 删除订单
     * @param orderId
     * @return
     */
    @DeleteMapping("/deleteOrder/{orderId}")
    public Result deleteOrder(@PathVariable Integer orderId){
        if (orderId==null) return null;
        //删除订单详情内的关联商品
        LambdaQueryWrapper<ZnOrderDetails> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnOrderDetails::getOrderId,orderId);
        iZnOrderDetailsService.remove(wrapper);
        // 删除订单
        boolean orderRemoved = znOrderService.removeById(orderId);
        if (orderRemoved) {
            return new Result(true, "删除成功", 200);
        } else {
            return new Result(false, "删除失败", 500);
        }
    }

    /**
     * 立即购买
     * @param znOrderBean
     * @param principal
     * @return
     */
    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody ZnOrderBean znOrderBean,Principal principal){
        System.out.println("-----------"+znOrderBean);
        if (znOrderBean==null) return new Result(false,"数据异常",400);
        znOrderService.buyNow(znOrderBean,principal.getName());
        return new Result(true,"成功",200);
    }

    /**
     * 去发货
     * @param znOrderBean
     * @return
     */
    @PutMapping("/deliverGoods")
    public Result upDeliverStatus(@RequestBody ZnOrderBean znOrderBean){
        if (znOrderBean.getOrderNumber()==null) return new Result(false,"数据异常",400);
        ZnOrder znOrder = znOrderService.getOneOrder(znOrderBean.getOrderNumber());
        znOrder.setCourierNumber(znOrderBean.getCourierNumber());
        znOrder.setLogisticsId(znOrderBean.getLogisticsId());
        znOrder.setDeliverStatus(1);
        znOrderService.updateById(znOrder);
        return new Result(true,"成功",200);
    }

    /**
     * 修改快递
     * @param znOrderBean
     * @return
     */
    @PutMapping("/updReceive")
    public Result updateReceive(@RequestBody ZnOrderBean znOrderBean){
        if (znOrderBean.getOrderNumber()==null && znOrderBean.getLogisticsId()==null) return new Result(false,"数据异常",400);
        ZnOrder znOrder = znOrderService.getOneOrder(znOrderBean.getOrderNumber());
        znOrder.setCourierNumber(znOrderBean.getCourierNumber());
        znOrder.setLogisticsId(znOrderBean.getLogisticsId());
        znOrderService.updateById(znOrder);
        return new Result(true,"成功",200);
    }

    /**
     * 订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/getOrderDetails/{orderId}")
    public Result getOrderDetailsList(@PathVariable Integer orderId){
        if (orderId==null) return new Result(false,"数据异常",400);
        List<ZnOrderDetailsBean> znOrderDetailsBeans = znOrderService.selectOrderDetails(orderId);
        return new Result(true,"成功",200,znOrderDetailsBeans);
    }

    /**
     * 用户申请退款
     * @param znOrderBean
     * @return
     */
    @PostMapping("/applyFor")
    public Result applyFor(@RequestBody ZnOrderBean znOrderBean){
        if (znOrderBean.getOrderNumber()==null) return new Result(false,"数据异常",400);
        ZnOrder znOrder = znOrderService.getOneOrder(znOrderBean.getOrderNumber());
        znOrder.setPayStatus(2);
        znOrderService.updateById(znOrder);
        return new Result(true,"成功",200);
    }
    /**
     * 用户收货
     * @param znOrderBean
     * @param principal
     * @return
     */
    @PostMapping("/takeDelivery")
    public Result takeDelivery(@RequestBody ZnOrderBean znOrderBean,Principal principal){
        if (znOrderBean.getOrderNumber()==null) return new Result(false,"数据异常",400);
        znOrderService.updateTakeStatus(znOrderBean.getOrderNumber(), principal.getName());
        return new Result(true,"成功",200);
    }
}